package paylayer

import (
	"strconv"
	//"strings"
	//"dhfshop/models/misc"
	"encoding/json"
	"dhfshop/models/isv"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"thirdpaysdk/api/oceanpayment"
	"dhfshop/service/redisgo"
	"github.com/pkg/errors"
)

func OrderOceanPay(appId,openId string,acPay *isv.AcPayParams,order *shopping.ValueOrder)(rsp PayResponse,prepayId string, err error) {
	account := acPay.MchId
	terminal := acPay.SubMchId
	appKey := acPay.Appkey
	currency := acPay.MediaId
	orderStr := strconv.FormatInt(order.OrderId,10)
	des := "订单" + "[" + orderStr + "]"
	noticeUrl := beego.AppConfig.String("WXPayNotify") + "/public/oceanpayment/notify"
	amount := strconv.FormatFloat(float64(order.Payment), 'f', -1, 32)
	firstName := "test"
	lastName := "test"
	ipAddr := "115.159.73.78"
	productName := des
	wxPayJsonStr, err := oceanpayment.MicroAppPay(account,terminal,noticeUrl,
	             orderStr,currency,amount,openId,firstName,lastName,ipAddr,productName,appKey)
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	//更新订单支付方式编号
	order.PayWayCode = 17
	shopping.UpdateOrderPayWayCode(order)
	rsp.WxpayRsp = wxPayJsonStr
	rsp.OrderId = orderStr
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderStr
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "ESHOP_ORDER_PAY"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func ChargeOceanPay(appId,openId,outTradeNo string,acPay *isv.AcPayParams,fee float32)(rsp PayResponse,prepayId string,err error){
	account := acPay.MchId
	terminal := acPay.SubMchId
	appKey := acPay.Appkey
	currency := acPay.MediaId
	
	des := "账户充值" + "[" + outTradeNo + "]"
	noticeUrl := beego.AppConfig.String("WXPayNotify") + "/public/oceanpayment/notify"
	amount := strconv.FormatFloat(float64(fee), 'f', -1, 32)
	firstName := "test"
	lastName := "test"
	ipAddr := "115.159.73.78"
	productName := des
	wxPayJsonStr, err := oceanpayment.MicroAppPay(account,terminal,noticeUrl,
	             outTradeNo,currency,amount,openId,firstName,lastName,ipAddr,productName,appKey)	 
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	rsp.WxpayRsp = wxPayJsonStr
	rsp.OrderId = outTradeNo
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + outTradeNo
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "MEMBER_CHARGE"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func CommonOceanPay(appId,openId,orderId,body,kind string,acPay *isv.AcPayParams,fee float32)(rsp PayResponse,prepayId string, err error){
    account := acPay.MchId
	terminal := acPay.SubMchId
	appKey := acPay.Appkey
	currency := acPay.MediaId
	noticeUrl := beego.AppConfig.String("WXPayNotify") + "/public/oceanpayment/notify"
	amount := strconv.FormatFloat(float64(fee), 'f', -1, 32)
	firstName := "test"
	lastName := "test"
	ipAddr := "115.159.73.78"
	productName := kind
	wxPayJsonStr, err := oceanpayment.MicroAppPay(account,terminal,noticeUrl,orderId,
	              currency,amount,openId,firstName,lastName,ipAddr,productName,appKey)
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	rsp.WxpayRsp = wxPayJsonStr
	rsp.OrderId = orderId
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderId
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = kind
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}
